using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using Common;

namespace PCR.PCRControls
{
	public partial class Section : UserControl, ISection, INotifyPropertyChanged, IComponentConnector
	{
		public static string DefaultStepName = Utility.GetMessage("step");

		private string _SectName;

		public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(Section), new FrameworkPropertyMetadata(false, OnIsSelectedChanged));

		private int _WhileNum = 1;

		private bool WhileNumEditClick;

		public ObservableCollection<Step> Items = new ObservableCollection<Step>();

		private bool Click;

		private bool _ScreenTest;

		public string SectName
		{
			get
			{
				return _SectName;
			}
			set
			{
				if (_SectName != value && value != null)
				{
					_SectName = value.Trim();
					OnPropertyChanged("SectName");
				}
			}
		}

		public bool IsSelected
		{
			get
			{
				return (bool)GetValue(IsSelectedProperty);
			}
			set
			{
				SetValue(IsSelectedProperty, value);
			}
		}

		public int WhileNum
		{
			get
			{
				return _WhileNum;
			}
			set
			{
				if (value < 1)
				{
					value = 1;
				}
				if (value > 99)
				{
					value = 99;
				}
				if (value == _WhileNum)
				{
					return;
				}
				List<Step> list = Items.Where((Step s) => s.Program).ToList();
				if (list.Count > 0)
				{
					int num = list.Select((Step s) => s.WhileNum).Max();
					if (value < num)
					{
						value = num;
					}
				}
				_WhileNum = value;
				OnPropertyChanged("WhileNum");
			}
		}

		public bool ScreenTest
		{
			get
			{
				return _ScreenTest;
			}
			set
			{
				Get().ScreenTest = value;
				if (_ScreenTest != value)
				{
					_ScreenTest = value;
					OnPropertyChanged("ScreenTest");
				}
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public Section()
		{
			RegisterEvent();
			InitializeComponent();
			Step step = new Step
			{
				SParent = this
			};
			Items.Add(step);
			cItems.Children.Add(step);
		}

		public void RegisterEvent()
		{
			Items.CollectionChanged += Items_CollectionChanged;
		}

		public void UnRegisterEvent()
		{
			Items.CollectionChanged -= Items_CollectionChanged;
		}

		private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			UpdateStepName();
		}

		private void UpdateStepName()
		{
			for (int i = 0; i < Items.Count; i++)
			{
				Step step = Items[i];
				if (string.IsNullOrEmpty(step.StepName))
				{
					step.StepName = DefaultStepName + (i + 1);
				}
				else if (step.StepName != DefaultStepName && step.StepName.StartsWith(DefaultStepName))
				{
					int result = 0;
					if (int.TryParse(step.StepName.Substring(DefaultStepName.Length, step.StepName.Length - DefaultStepName.Length), out result))
					{
						step.StepName = DefaultStepName + (i + 1);
					}
				}
			}
		}

		public void OnPropertyChanged(string propName)
		{
			if (string.IsNullOrEmpty(propName))
			{
				throw new ArgumentException("propName");
			}
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
			}
		}

		private static void OnIsSelectedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			Section section = obj as Section;
			if (section.IsSelected)
			{
				if (!section.Items.Any((Step a) => a.IsSelected))
				{
					section.Background = new SolidColorBrush(Colors.LightPink);
				}
				return;
			}
			section.Background = new SolidColorBrush(Colors.Transparent);
			foreach (Step item in section.Items)
			{
				item.IsSelected = false;
			}
		}

		private void WhileNumEdit_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			WhileNumEditClick = true;
			e.Handled = true;
		}

		private void WhileNumEdit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			if (WhileNumEditClick)
			{
				cWhileNum1.Visibility = Visibility.Collapsed;
				cWhileNum.Visibility = Visibility.Visible;
				cWhileNum.Focus();
			}
		}

		private void cWhileNum_LostFocus(object sender, RoutedEventArgs e)
		{
			cWhileNum.Visibility = Visibility.Collapsed;
			cWhileNum1.Visibility = Visibility.Visible;
		}

		public Step Get(int index = -1)
		{
			if (index == -1 && IsSelected)
			{
				return Items.Where((Step s) => s.IsSelected).FirstOrDefault();
			}
			if (index > -1 && index < Items.Count)
			{
				return Items[index];
			}
			return null;
		}

		public void Add(bool after = false)
		{
			if (!IsSelected)
			{
				return;
			}
			Step step = Items.Where((Step s) => s.IsSelected).FirstOrDefault();
			if (step != null && Items.Count < 9)
			{
				Step step2 = new Step();
				step2.SParent = this;
				int num = Items.IndexOf(step) + (after ? 1 : 0);
				Items.Insert(num, step2);
				cItems.Children.Insert(num, step2);
				if (num > 0)
				{
					Binding binding = new Binding("Temperature");
					binding.Source = Items[num - 1];
					binding.Mode = BindingMode.OneWay;
					step2.SetBinding(Step.PriorTemptProperty, binding);
				}
				if (num < Items.Count - 1)
				{
					Binding binding2 = new Binding("Temperature");
					binding2.Source = step2;
					binding2.Mode = BindingMode.OneWay;
					Items[num + 1].SetBinding(Step.PriorTemptProperty, binding2);
				}
			}
		}

		public void AddLast()
		{
			Step step = Items.LastOrDefault();
			if (step != null && Items.Count < 9)
			{
				Step step2 = new Step();
				step2.SParent = this;
				int num = Items.Count - 1;
				Items.Add(step2);
				cItems.Children.Add(step2);
				if (num >= 0)
				{
					Binding binding = new Binding("Temperature");
					binding.Source = Items[num];
					binding.Mode = BindingMode.OneWay;
					step2.SetBinding(Step.PriorTemptProperty, binding);
				}
			}
		}

		public bool Remove()
		{
			if (IsSelected)
			{
				Step step = Items.Where((Step s) => s.IsSelected).FirstOrDefault();
				if (step != null && Items.Count > 1)
				{
					int num = Items.IndexOf(step);
					Items.Remove(step);
					cItems.Children.Remove(step);
					IsSelected = false;
					if (num > 0 && num < Items.Count)
					{
						Binding binding = new Binding("Temperature");
						binding.Source = Items[num - 1];
						binding.Mode = BindingMode.OneWay;
						Items[num].SetBinding(Step.PriorTemptProperty, binding);
					}
					return true;
				}
			}
			return false;
		}

		private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			Click = true;
		}

		private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			if (!Click)
			{
				return;
			}
			Click = false;
			Step step = e.Source as Step;
			if (step == null)
			{
				Step step2 = Get();
				if (step2 != null)
				{
					step2.IsSelected = false;
					IsSelected = true;
					base.Background = new SolidColorBrush(Colors.LightPink);
				}
				else
				{
					IsSelected = !IsSelected;
				}
				return;
			}
			IsSelected = step.IsSelected;
			if (!step.IsSelected)
			{
				return;
			}
			base.Background = new SolidColorBrush(Colors.Transparent);
			foreach (Step item in Items)
			{
				if (item != step && item.IsSelected)
				{
					item.IsSelected = false;
					break;
				}
			}
		}

		public bool EditProgram()
		{
			Step step = Items.Where((Step s) => s.IsSelected).FirstOrDefault();
			if (step != null)
			{
				return step.EditProgram();
			}
			return false;
		}

		[DebuggerNonUserCode]
		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		internal Delegate _CreateDelegate(Type delegateType, string handler)
		{
			return Delegate.CreateDelegate(delegateType, this, handler);
		}
	}
}
